package com.shop2cn.iapi.couponmanage.testcase.old;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.service.CreateCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.CouponShop2cn.couponconversion;
import com.ymttest.database.model.CouponShop2cn.couponconversionbatch;
import com.ymttest.database.model.CouponShop2cn.couponconversioncode;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponconversionWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponconversionbatchWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponconversioncodeWapper;


/**
 * 创建兑换码单据 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Create {
	private static CreateBean createBean;
	private static CreateCall createCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建兑换码单据");
	}

	@Before
	public void caseUp() {
		createBean = new CreateBean();
		createCall = new CreateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_001() {
		Logger.start(true, "创建兑换码随机单据-单batchcode");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());
			//batchCodes.add("20004026");

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_002() {
		Logger.start(true, "创建兑换码自定义单据-单batchcode");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			createCouponBean.setCouponNum(1);
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());
			
			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_003() {
		Logger.start(true, "创建兑换码单据-batchcode不存在-调用服务的人验证,这边不验证");
		try {

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add("sdsdsd");

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains(true, createCall.getMessage().contains("批次不存在"), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_004() {
		Logger.start(true, "创建兑换码随机单据-多条batchcode");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall1 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall2 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall3 = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall1.getBatchCode());
			batchCodes.add(createCouponCall2.getBatchCode());
			batchCodes.add(createCouponCall3.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_005() {
		Logger.start(true, "创建兑换码自定义单据-多条batchcode");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			createCouponBean.setCouponNum(1);
			CreateCouponCall createCouponCall1 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall2 = couponCallService.createPlatformCoupon(createCouponBean);
			CreateCouponCall createCouponCall3 = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();

			batchCodes.add(createCouponCall1.getBatchCode());
			batchCodes.add(createCouponCall2.getBatchCode());
			batchCodes.add(createCouponCall3.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_Create_006() {
		Logger.start(true, "创建兑换码随机单据-单batchcode-压测生成100W张兑换码");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);
			createBean.setGenerateQuantity(1000000);
			createCall.setData(createBean);
			createCall.callService();

			Date d1 = new Date();

			Logger.comment("开始时间："+d1.toLocaleString());
			
			couponconversion bill = new couponconversionWapper()
					.selectByconversionName(createBean.getBillName()).get(0);
			
			List<couponconversioncode> couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
			int flag = 0;
			while (couponconversioncodes.size()!= 100000 && flag < 50) {
				Thread.sleep(60000);
				couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
				
				flag++;
				
				Logger.comment("第"+flag+"分钟共生成"+couponconversioncodes.size()+"条记录，当前时间："+new Date().toLocaleString());				
			}
	
			Date d2=new Date();
			Logger.comment("总共花费时间:"+(d2.getTime()-d1.getTime())/(1000*60)+"分钟。");


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_007() {
		Logger.start(true, "创建兑换码随机单据-创建指定兑换码的时候验证兑换码是否已经存在");
		try {

			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);

			createCall.setData(createBean);
			createCall.callService();
			
			createCall.setData(createBean);
			createCall.callService();

			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains(createCouponCall.getBatchCode()+"曾创建过兑换单!", createCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_008() {
		Logger.start(false, "创建兑换码自定义单据-兑换张数大于1-返回:错误");
		try
		{
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
	
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);
	
			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();
	
			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());
	
			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			createBean.setGenerateQuantity(2);
			createCall.setData(createBean);
			createCall.callService();
			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains(true, createCall.getMessage().contains("指定兑换码方式数量必须为1"), "验证返回Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_009() {
		Logger.start(false, "创建兑换码自定义单据-batchType=2是创建套券(batchCodes应该是套券code)");
		try
		{
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			createBean.setBatchType(2);
			createCall.setData(createBean);
			createCall.callService();
			
			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains(true, createCall.getMessage().contains("批次不存在"), "验证返回Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Create_010() {
		Logger.start(false, "创建兑换码随机单据-batchType=2是创建套券(batchCodes应该是套券code)");
		try
		{
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			createBean = mCouponserviceCallService.setDefaultRandomCreateBean(batchCodes);
			createBean.setBatchType(2);
			createCall.setData(createBean);
			createCall.callService();
			
			Logger.verifyEquals("2", createCall.getCode(), "验证返回code");
			Logger.verifyIsContains(true, createCall.getMessage().contains("批次不存在"), "验证返回Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn() throws InterruptedException {
		Logger.verifyEquals("0", createCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", createCall.getMessage(), "验证返回Message");

		// 验证couponconversion表
		List<couponconversion> bills = new couponconversionWapper()
				.selectByconversionName(createBean.getBillName());

		Logger.verifyEquals(1, bills.size(), "验证数据couponconversion中生成一条相应记录");

		if (bills.size() == 1) {
			couponconversion bill = bills.get(0);
			Logger.verifyEquals(createBean.getBillDes(), bill.getConversionDes(), "验证数据couponconversion中字段ConversionDes");
			Logger.verifyEquals(createBean.getGenerateMethod(), bill.getGenerateMethod().intValue(),
					"验证数据couponconversion中字段GenerateMethod");
			Logger.verifyEquals(createBean.getCreateUser(), bill.getCreateUser(),
					"验证数据couponconversion中字段CreateUser");
			Logger.verifyEquals(createBean.getGenerateQuantity(), bill.getGenerateQuantity(),
					"验证数据couponconversion中字段GenerateQuantity");

			// 验证Ymt_CouponConversionBatchRelation表
			List<couponconversionbatch> relations = new couponconversionbatchWapper()
					.selectByConversionId(bill.getConversionId());

			Logger.verifyEquals(createBean.getCouponBatchCodes().size(), relations.size(), "验证数据couponconversionbatch中生成一条相应记录");

			List<String> batchCodes = new ArrayList<>();
			
			for (couponconversionbatch relation : relations) {
				batchCodes.add(relation.getBatchCode());
			}
			
			Logger.verifyEquals(true, isListEqual(batchCodes,createBean.getCouponBatchCodes()), "验证数据couponconversionbatch中生成一条相应记录");
			
			// 验证couponconversion表
			List<couponconversioncode> couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
//			Logger.verifyEquals(createBean.getGenerateQuantity(), couponconversioncodes.size(), "验证数据couponconversion中生成一条相应记录");

			// 自定义
			if(createBean.getGenerateMethod()==1)
			{
				Logger.verifyEquals(1, couponconversioncodes.size(), "验证数据couponconversion中生成一条相应记录");
				Logger.verifyEquals(createBean.getCustomConversionCode(), couponconversioncodes.get(0).getConversionCode(), "验证数据couponconversion中ConversionCode");

			}
			//随机生成
			else {
				
				int flag=0;
				
				while(couponconversioncodes.size()<createBean.getGenerateQuantity() && flag<100)
				{
					flag++;
					Thread.sleep(200);
					couponconversioncodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());	
				}
				
				Logger.verifyEquals(createBean.getGenerateQuantity(), couponconversioncodes.size(), "验证数据couponconversion中生成相应记录");
				
				for (couponconversioncode couponconversioncode : couponconversioncodes) {
					Logger.verifyNotNull(couponconversioncode.getConversionCode(), "验证数据couponconversion中Conversioncode");
				}
				

			}
		}

	}
	
	private boolean isListEqual(List<String> list1, List<String> list2)
	{
		for (String str : list2) {
			if(!list1.contains(str))
				return false;
		}
		for (String str : list1) {
			if(!list2.contains(str))
				return false;
		}
		return true;
	}
	
}